package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetMealService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetMealDishService setMealDishService;

    @Override
    public Page<SetmealDto> selectPageByName(Integer page, Integer pageSize, String name) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<Setmeal>().like(!StringUtils.isEmpty(name), "name", name);
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        page(setmealPage, queryWrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage);
        List<SetmealDto> collect = setmealPage.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(collect);
        return setmealDtoPage;
    }

    @Override
    public void saveWithDishes(SetmealDto setmealDto) {
        save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(item -> item.setSetmealId(setmealDto.getId()));
        System.out.println(setmealDishes);
        setMealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto selectById(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());

        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<SetmealDish>().eq("setmeal_id", setmeal.getId());
        List<SetmealDish> list = setMealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        setMealDishService.remove(new QueryWrapper<SetmealDish>().eq("setmeal_id", setmealDto.getId()));
        setmealDto.getSetmealDishes().stream().forEach(item -> {
            item.setSetmealId(setmealDto.getId());
        });

        setMealDishService.saveBatch(setmealDto.getSetmealDishes());

    }

    @Override
    public void updateStatusByIds(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            updateById(setmeal);
        }
    }

    @Override
    @Transactional
    public void deleteWithDishes(Long[] ids) {

        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("status", 1)
                .in("id", Arrays.asList(ids));
        int count = this.count(queryWrapper);
        if (count >0 ) {
            throw new CustomException("在售状态");
        }
        for (Long id : ids) {
            setMealDishService.remove(new QueryWrapper<SetmealDish>().eq("setmeal_id", id));
            this.removeById(id);
        }
    }
}
